#coding=utf-8
import os
import shutil
import urllib
import urllib2
import time
import multiprocessing
from pyquery import PyQuery as pq
#获取图片网址列表
def getImgUrls(pageUrl):
 req=urllib2.Request(pageUrl)
 response=urllib2.urlopen(req).read()
 doc=pq(response)
 fans_count=doc(".ex-userinfo div.span:eq(1) span").text()
 username = doc(".ex-userinfo div.ex-username span:eq(0)").text()
 person_cate=doc(".ex-userinfo div.ex-username span:eq(1)").text()
 cup=doc(".ex-userinfo div.span:eq(0) span").text()
 imgs=doc('.details-info img')
 imgUrls=[]
 host="https://wxpub.o2bra.com.cn"
 if int(fans_count)>500 :
     #温衡#民咕姑姑#陈岩燕#WiSH#子期无忧
   for img in imgs:
    imgUrls.append(host+doc(img).attr.src)
 return imgUrls,username,person_cate,cup
#保存图片到本地
def saveImg(imgs,dir,num):
    if(os.path.exists(dir)==False):
      make_dir(dir)
    for imgUrl in imgs :
        img=urllib.urlopen(imgUrl).read()
        img_path=dir+str(num)+"_"+str(time.time())+".jpg"
        f = file(img_path,"wb")
        f.write(img)
    f.close()
#开始下载图片
def get_imgs(index,start,end):

    for i in range(start,end):
        try:
            url=index+str(i)
            imgUrls = getImgUrls(url)[0]
            username = getImgUrls(url)[1]
            person_cate = getImgUrls(url)[2]
            cup = getImgUrls(url)[3]
            sec_dir = username +person_cate + cup
            dir="D://ox/"+"imgs"+"/"+sec_dir+"/"
            saveImg(imgUrls,dir,i)
            print url
        except:
            continue
#程序入口
def start(url,count,start,end):
    process_list=[]
    index=(end-start)/count
    for i in range(count):
        process_list.append(multiprocessing.Process(target = get_imgs, args = (url,start+index*i,start+index*(i+1))))
    process_list.append(multiprocessing.Process(target = get_imgs, args = (url,start+index*i,end+1)))
    for process in process_list:
        process.start()

def make_dir(path):
    if  not os.path.exists(path):
        #shutil.rmtree(path)
        os.mkdir(path)
def deleteEmptyDir(path):
    files = os.listdir(path)  # 获取路径下的子文件(夹)列表
    for file in files:
        file = os.path.join(path,file)
        if os.path.isdir(file):  # 如果是文件夹
            print file
            if not os.listdir(file):  # 如果子文件为空
                print "删除",file
                os.rmdir(file)  # 删除这个空文件夹
            else:
                deleteEmptyDir(file)
        elif os.path.isfile(file):  # 如果是文件
            if os.path.getsize(file) <= 1024:  # 文件大小为0
                os.remove(file)  # 删除这个文件
                print "删除",file

if __name__ == "__main__":
    # url = "https://wxpub.o2bra.com.cn/blogs/mobile/"
    url = "http://47.94.182.9/blogs/mobile/"
    begin =0
    end = 5000
    #dir="D://ox/"+str(time.time())+"/"
    #make_dir(dir)
    start(url,20,begin,end)
